<?php
/**
 * Created by PhpStorm.
 * User: 唐轶俊
 * Date: 2017/4/26
 * Time: 15:08
 */
namespace app\admin\controller;
use app\data\Data;
use app\validate\TypeValidate;
use think\Request;

/**
 * +-------------------------------------
 * Class Type
 * @package app\admin\controller
 * 栏目管理类
 * +-------------------------------------
 */
class Type extends Check{
    /**
     * @return array|\think\response\View
     * 添加栏目
     */
    public function typeAdd(){
        $type = new Data('type');
        try{
            if(Request::instance()->isPost()){
                $post = Request::instance()->post();
                $rule_validate = new TypeValidate();
                $rule_validate = $rule_validate->typeValidate(); //获取验证规则
                self::validateData($rule_validate['rule'],$rule_validate['message'],$post);
                $type_add_bool = $type->insertData($post); //插入数据
                if(!$type_add_bool){
                    throw new \LogicException(Config::get('error_table.error')['msg'],Config::get('error_table.error')['code']);
                }
                return self::formatSuccessResult();
            }
        }catch(\Exception $e){
            return self::formatResult($e->getCode(),$e->getMessage());
        }
        $parent_type = $type->selectData("p_id = 0");
        return view('typeAdd',['parent_type'=>$parent_type]);
    }

    /**
     * @return \think\response\View
     * 栏目列表
     */
    public function typeList(){
        $type = new Data('type');
        $list = $type->selectData();
        $list = infinite($list,0,0);
        return view('typeList',['list'=>$list]);
    }

    /**
     * @return \think\response\View
     * 更新栏目
     */
    public function typeSave(){
        $type = new Data('type');
        try{
            if(Request::instance()->isPost()){
                $post = Request::instance()->post();
                $rule_validate = new TypeValidate();
                $rule_validate = $rule_validate->typeValidate(); //获取验证规则
                self::validateData($rule_validate['rule'],$rule_validate['message'],$post);
                $type_save_bool = $type->updateData($post);
                if($type_save_bool === false){
                    throw new \LogicException(Config::get('error_table.error')['msg'],Config::get('error_table.error')['code']);
                }
                return self::formatSuccessResult();
            }
        }catch(\Exception $e){
            return self::formatResult($e->getCode(),$e->getMessage());
        }
        $id = input('id');
        $getOne = $type->find("id = $id");
        $parent_type = $type->selectData("p_id = 0");
        return view('typeSave',['parent_type'=>$parent_type,'one'=>$getOne]);
    }

    /**
     * 删除栏目数据
     */
    public function typeDelete(){
        try{
            if(Request::instance()->isPost()){
                $type = new Data('type');
                $post = Request::instance()->post();
                $id = $post['id'];
                $type_delete_bool = $type->deleteByWhere("id = $id");
                if(!$type_delete_bool){
                    throw new \LogicException(Config::get('error_table.error')['msg'],Config::get('error_table.error')['code']);
                }
            }
        }catch(\Exception $e){
            return self::formatResult($e->getCode(),$e->getMessage());
        }
        return self::formatSuccessResult();
    }
}